package cn.tedu.jsd2210.csmall.passport.controller;

import cn.tedu.jsd2210.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.jsd2210.csmall.passport.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.jsd2210.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.jsd2210.csmall.passport.security.AdminDetails;
import cn.tedu.jsd2210.csmall.passport.security.LoginPrincipal;
import cn.tedu.jsd2210.csmall.passport.service.IAdminService;
import cn.tedu.jsd2210.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags="01.管理员管理模块")

public class AdminController {
    @Autowired
    private IAdminService service;

    public AdminController() {
        log.debug("创建控制器类对象：AdminController");
    }

    // http://localhost:9081/admins/login
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoDTO);
        String jwt = service.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9081/admins/add-new
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);
        service.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9527/delete
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "管理员ID",required = true,dataType = "long")
    @PostMapping("{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改管理员状态的请求,参数:{}",id);
        service.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改管理员状态的请求,参数:{}",id);
        service.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins
    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(
//            @ApiIgnore @AuthenticationPrincipal AdminDetails adminDetails){
            //注意:此处当事人为认证信息中的当事人即可
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员列表】的请求，无参数");
        log.debug("当事人信息:{}",loginPrincipal);
        log.debug("当事人信息中的ID:{}",loginPrincipal.getId());
        log.debug("当事人信息中的用户名:{}",loginPrincipal.getUsername());
        List<AdminListItemVO> list = service.list();
        return JsonResult.ok(list);
    }
}

